<template>
  <div>
    <div class="playmain">

      <div class="cateListBox" v-show="isShowList">
        <div class="hd">
          <i class="icn"></i>
        </div>
        <div class="bd">
          <h3><a @click="goCatList('全部')">全部风格</a></h3>
          <li class="cb clearfix" v-for="(item,index) in catList" :key="index"   :class="{last:index===4}">
            <!-- 种类 -->
            <div class="kind">
              <i class="icn0" v-if="index === 0"></i>
              <i class="icn1" v-else-if="index === 1"></i>
              <i class="icn2" v-else-if="index === 2"></i>
              <i class="icn3" v-else-if="index === 3"></i>
              <i class="icn4" v-else></i>
              <span>{{item.name}}</span>
            </div>
            <!-- 种类详情 -->
            <div class="item">
                <div v-for="(attr,index2) in item.sub" :key="index2">
                  <a class="sep" @click="goCatList(attr)">{{attr}}</a>
                  <span class="line">|</span>
                </div>
        </div>
        </li>
      </div>
      <div class="ft"></div>
    </div>

        <div class="g-wrap">
          <div class="u-title">
            <h3>
              <span>{{ cat }}</span>
              <a href="javascript:;" @click="changeShow">
                <i>
                  选择分类
                  <em class="jiantou"></em>
                </i>
              </a>
            </h3>
            <div class="u-btn u-btn-hot">
              <a href="javascript:;">热门</a>
            </div>
          </div>
        </div>
        <ul class="m-cvrlst">
          <li v-for="(item,index) in playList" :key="index">
            <div class="u-cover">
              <img v-lazy="item.coverImgUrl" alt="" :key="index" :title="item.name">
              <router-link :to="{name: 'RecommendDetail', params: {id: item.id}}" :title="item.name" class="u-name" ></router-link>
              <div class="bottom">
                <a href="javascript:;" @click.prevent="addSong(item.id)" title="播放"></a>
                <span class="erji"></span>
                <span class="people-num">{{ formatNumber(item.playCount) }}</span>
              </div>
            </div>
            <p class="first">
              <router-link :to="{name: 'RecommendDetail', params: {id: item.id}}" :title="item.name">{{ item.name }}</router-link>
            </p>
            <p class="second" v-if="item.creator">
              <span style="color: #999;">by</span>
              <router-link :to="{name:'UserDetailView',params:{id:item.creator.userId,type:1}}" :title="item.creator.nickname">{{ item.creator.nickname }}</router-link>
              <img v-if="item.creator.avatarDetail"
                   :src="item.creator.avatarDetail.identityIconUrl"
                   alt="">
            </p>
          </li>
        </ul>
        <div style="height: 30px; width: 100%;"></div>
        <Pagination :pageNo='pageNo' :pageSize='pageSize' :total='total' :continues='6'
                    @getPageNo="getPageNo"></Pagination>
      </div>
    </div>
</template>

<script>
import Header from "@/components/Header";
import Footer from "@/components/Footer";
import SubNav from "@/components/SubNav";
import Pagination from "@/components/Pagination";
import {formatNumber} from "@/utils/formatNumber";
import {reqSongListDetail} from "@/http/api";

export default {
  name: "index",
  components: {
    Header, Footer, SubNav, Pagination
  },
  data() {
    return {
      formatNumber: formatNumber,
      cat: '全部',
      pageNo: 1,
      pageSize: 35,
      total: 100,
      // 主体中的数据
      playList: [],
      // 弹框中的数据
      catList: [],
      //是否显示弹窗
      isShowList: false,
      catCopy: '全部',
    }
  },
  methods: {
    async addSong(id) {
      let result = await reqSongListDetail(id)
      let list = result.data.playlist.tracks
      this.$store.commit('track/addMusic', list)
      this.$store.commit('track/SETMUSIC', list[0])
    },
    goCatList(name){
      this.$router.push({name:'PlayList',params:{name:name}})
      // window.location.reload()
    },
    changeShow(){
      this.isShowList = !this.isShowList
    },
    //获取全部歌单列表的名称
    async getCatList(){
      let result = await this.$API.reqCatList();
      for(let i = 0;i <= 4;i++){
        let x = {name:'',sub:[]};
        if(i === 0) x.name="语种";
        else if(i === 1) x.name = "风格";
        else if(i === 2) x.name = "场景";
        else if(i === 3) x.name = "情感";
        else x.name = "主题";
        this.catList.push(x);
      }
      result.data.sub.forEach((item)=>{
        this.catList[item.category].sub.push(item.name);
      })
    },
    SendHotRecommend(id) {
      this.$router.push({name: 'RecommendDetail', params: {id: id}})
    },
    async getCat() {
      let result = await this.$API.reqPlayList(
          this.pageSize, (this.pageNo - 1) * this.pageSize, this.cat, 'hot'
      );
      this.total = result.data.total;
      this.playList = result.data.playlists;
    },
    //获取当前页数
    getPageNo(pageNo) {
      this.pageNo = pageNo;
      document.body.scrollTop = document.documentElement.scrollTop = 0;
      this.getCat();
    },
  },
  mounted() {
    this.$store.dispatch('playList/GetPlayList', {
      limit: this.pageSize,
      order: 'hot',
      cat: this.cat,
      offset: (this.pageNo - 1) * this.pageSize
    })

    this.getCat()
    this.getCatList()
  },
  // activated() {
  //   window.location.reload()
  // },
  created() {
    this.cat = this.$route.params.name
  }
}
</script>

<style scoped lang="less">
.playmain {
  width: 980px;
  height: auto;
  padding: 40px 0 40px 0;
  overflow: hidden;
  min-height: 700px;
  margin: 0 auto;
  background-color: #fff;
  border: 1px solid #d3d3d3;

  //弹出框
  .cateListBox {
    width: 720px;
    height: 413px;
    position: absolute;
    top: 180px;
    left: 232px;
    z-index: 5;
    width: 720px;

    .hd {
      width: 100%;
      height: 32px;
      background: url("./images/sltlyr.png") no-repeat;

      .icn {
        position: absolute;
        top: 2px;
        left: 132px;
        display: inline-block;
        width: 24px;
        height: 11px;
        background: url("./images/iconall.png") no-repeat;
        background-position: -48px 0;
      }
    }

    .bd {
      width: 720px;
      height: 400px;
      //padding: 0 10px;
      background: url("./images/sltlyr.png") repeat-y -720px 0;

      h3 {
        height: 38px;
        padding-left: 40px;
        border-bottom: 1px solid #e6e6e6;
        font-weight: normal;
        display: block;
        color: #333;
        border-bottom: 1px solid #e6e6e6;
        font-family: Arial, Helvetica, sans-serif;
        -webkit-text-size-adjust: none;

        a {
          width: 75px;
          height: 26px;
          text-align: center;
          display: inline-block;
          line-height: 26px;
          background: url('./images/button.png') no-repeat 0 -64px;
          font-size: 12px;
          color: #333;

          &:hover {
            cursor: pointer;
            text-decoration: underline;
            color: #333;
          }
        }
      }

      .cb {
        width: 100%;
        display: flex;

        .kind {
          float: left;
          width: 70px;
          height: 43px;
          margin-left: 26px;
          padding-top: 20px;
          font-weight: bold;

          i {
            display: block;
            float: left;
            width: 23px;
            height: 23px;
            margin: 0px 8px 4px 0;
            background: url('./images/icon.png') no-repeat;
          }

          .icn0 {
            background-position: -20px -735px;
          }

          .icn1 {
            background-position: 0 -60px;
          }

          .icn2 {
            background-position: 0 -88px;
          }

          .icn3 {
            background-position: 0 -117px;
          }

          .icn4 {
            background-position: 0 -141px;
          }

          span {
            font-size: 12px;
            color: #333;
            font-family: Arial, Helvetica, sans-serif;
            -webkit-text-size-adjust: none;
            line-height: 29px;
          }
        }

        .item {
          float: left;
          padding: 21px 15px 0 15px;
          border-left: 1px solid #e6e6e6;
          line-height: 24px;
          width: 603px;

          div {
            line-height: 24px;
            display: inline-block;

            .sep {
              color: #333;

              &:hover {
                color: #333;
                cursor: pointer;
                text-decoration: underline;
              }
            }

            .line {
              margin: 0 9px 0 11px;
              color: #d8d8d8;
            }
          }

        }
      }

      .last {
        height: 102px;

        .kind {
          padding-top: 24px;
        }

        .item {
          height: 100%;
        }
      }
    }

    .ft {
      width: 100%;
      height: 20px;
      background: url("./images/sltlyr.png") repeat-y -1440px -12px;
    }
  }

  .g-wrap {
    width: 980px;
    margin: 0 auto;

    .u-btn-hot {

      background-image: url("../../assets/button.png");
    }

    .u-title {
      width: 900px;
      height: 42px;
      margin: 0 auto;
      display: flex;
      align-items: center;
      border-bottom: 2px solid #C20C0C;

      h3 {
        //width: 151px;
        height: 33px;
        float: left;
        margin-left: 12px;

        span {
          font-family: "Microsoft Yahei", Arial, Helvetica, sans-serif;
          font-size: 24px;
          font-weight: normal;
          float: left;
        }

        a {
          margin-left: 10px;
          border: 1px solid #C3C3C3;
          border-radius: 5px;
          display: inline-block;
          width: 91px;
          height: 31px;
          text-align: center;
          line-height: 31px;
          float: left;
          background-color: #F6F6F6;

          i {
            color: #0c73c2;
            font-weight: normal;

            em {
              display: inline-block;
              width: 8px;
              height: 5px;
              background: url("../../assets/icon.png") -70px -543px;
            }
          }
        }
      }

      .u-btn {
        margin-left: auto;
        width: 46px;
        height: 29px;
        text-align: center;
        line-height: 29px;
        border-radius: 3px;
        background-position: 0 0;
        background-color: #CA0C0E;

        a {
          color: white;
        }
      }
    }
  }

  .m-cvrlst {
    width: 950px;
    padding-top: 20px;
    margin: 0 auto;
    height: auto;
    overflow: hidden;

    li {
      width: 190px;
      height: 218px;
      float: left;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-content: center;

      .u-cover {
        width: 140px;
        height: 140px;
        margin: 0 auto;
        position: relative;

        .u-name {
          width: 100%;
          height: 100%;
          position: absolute;
          top: 0;
          left: 0;
          display: inline-block;
        }

        .bottom {
          width: 140px;
          height: 27px;
          position: absolute;
          bottom: -2px;
          left: 0;
          background: url("../../assets/coverall.png") no-repeat 0 -540px;

          a {
            display: inline-block;
            width: 16px;
            height: 17px;
            background: url("../../assets/iconall.png") 0 0;
            position: absolute;
            right: 10px;
            bottom: 6px;
          }

          .erji {
            width: 14px;
            height: 11px;
            display: inline-block;
            position: absolute;
            bottom: 9px;
            left: 10px;
            background: url("../../assets/iconall.png") no-repeat 0 -24px;
          }

          .people-num {
            position: absolute;
            bottom: 7px;
            left: 30px;
            color: #ccc;
          }
        }

        img {
          width: 140px;
          height: 140px;
        }

        a {
          width: 140px;
          display: inline-block;
        }
      }

      .first {
        width: 140px;
        margin: 6px auto;
        font-size: 14px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;

        a {
          font-size: 14px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          display: block;
          max-width: 100%;
          color: #000;

          &:hover {
            text-decoration: underline;
          }
        }
      }

      .second {
        margin: 0 auto;
        width: 140px;
        display: flex;
        align-items: center;

        a {
          color: #333;
          margin: 0 7px;
          max-width: 70%;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }

        img {
          width: 13px;
          height: 13px;
        }
      }

    }
  }
}
</style>